﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EntityComponentSystem.Framework.Systems
{
    public class BehaviorSystem : IBehaviorSystem
    {
        private class SystemBehaviorHelper
        {
            public int Entity;
            public Behavior Behavior;
            public Action Action;
        }

        private List<SystemBehaviorHelper> actions = new List<SystemBehaviorHelper>();

        public string Name { get; protected set; }

        public virtual void Update(GameEngine ge)
        {
            foreach (var action in actions)
                action.Action();
        }

        public virtual void Init(GameEngine ge)
        {

        }

        public void AddBehavior(int entity, Behavior behavior, Action action)
        {
            actions.Add(new SystemBehaviorHelper() { Entity = entity, Behavior = behavior, Action = action });
        }

        public void RemoveBehavior(int entity, Behavior behavior)
        {
            for (int i = actions.Count - 1; i >= 0; i--)
            {
                if (actions[i].Entity == entity && actions[i].Behavior == behavior)
                    actions.RemoveAt(i);
            }
        }
    }
}
